package com.lone.smallmovie.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

public class MessageTools 
{
	
	public static Log log = LogFactory.getLog(MessageTools.class);

	public static void send(HttpServletRequest request, HttpServletResponse response, Map<String, Object> message)
	{
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
		log.info("message send start ........");
		
		PrintWriter out = null;
		JSONObject jsObj = new JSONObject( message);
		
	    try 
	    {
			out = response.getWriter();
			// out.write(AES.AES_en(jsObj.toString(), AES.AES_KEY));
			out.write(jsObj.toString());
			log.info("message send is :" + jsObj.toString());
		} 
	    catch (IOException e) 
		{
		  log.error("updata error....................................."+e);
		}
		finally 
		{
			out.close();
		}
	    
		
	}

    @RequestMapping(produces="text/html;charset=UTF-8")
    @ResponseBody
	public static Map<String,Object> send(Map<String,Object> map){

	    return map;

    }
	
	

	
	
	public static void sendArray(HttpServletRequest request, HttpServletResponse response, List array)
	{
		
		log.info("message sendArray start ........");
		
		PrintWriter out = null;
		JSONArray jsObj = new JSONArray(array);
		
	    try 
	    {
	    	
	    	response.setCharacterEncoding("UTF-8");
	    	
			out = response.getWriter();
			// out.write(AES.AES_en(jsObj.toString(), AES.AES_KEY));
			out.write(jsObj.toString());
			log.info("message sendArray is :" + jsObj.toString());
		} 
	    catch (IOException e) 
		{
		  log.error("updata error....................................."+e);
		}
		finally 
		{
			out.close();
		}
	    
		
	}
	
	
	public static void sendString(HttpServletRequest request, HttpServletResponse response, String message)
	{
		
		log.info("message sendArray start ........");
		
		PrintWriter out = null;
		
	    try 
	    {
	    	
	    	response.setCharacterEncoding("UTF-8");
	    	
			out = response.getWriter();
			out.write(message);
			log.info("message sendArray is :" + message);
		} 
	    catch (IOException e) 
		{
		  log.error("updata error....................................."+e);
		}
		finally 
		{
			out.close();
		}
	    
		
	}
	
	
}
